﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using System.Threading;
using System.Threading.Tasks;
using DevExpress.XtraBars;
using MainApp.Helper;
using MainApp.Forms;

namespace MainApp
{
    public partial class MainWindow : DevExpress.XtraEditors.XtraForm
    {
        public MainWindow()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
            //Form form = new FormProfession();
            //form.MdiParent = this;
            //form.Show();
            //form = new FormNation();
            //form.MdiParent = this;
        }

        public void StartProgress()
        {
            this.repositoryItemMarqueeProgressBar1.Stopped = false;
            this.repositoryItemMarqueeProgressBar1.Paused = false;
        }

        public void StopProgress()
        {
            this.repositoryItemMarqueeProgressBar1.Stopped = true;
            this.repositoryItemMarqueeProgressBar1.Paused = true;
        }

        public void ShowMessage(string text)
        {
            this.barMessage.Caption = text;
            //this.Update();
        }

        public void ShowAddProductForm()
        {
            barButtonItem7_ItemClick(null, null);
        }

        public void Merge(Bar bar)
        {
            for (var i = 0; i < bar.ItemLinks.Count; i++)
            {
                this.ToolBar.AddItem(bar.ItemLinks[i].Item);
            }
            bar.Visible = false;
        }

        public void UnMerge(Bar bar)
        {
            this.ToolBar.Reset(); bar.Visible = true;
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            barButtonItem14_ItemClick(null, null);
        }

        //save
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                ActiveMdiChild.BeginInvoke(new Action(() =>
                {
                    btnSave.Enabled = false;
                    Program.ShowMessage("开始保存数据...");
                    Program.StartProgress();
                    try
                    {
                        var result = ((IChildForm)ActiveMdiChild).Save();

                        if (result.Message.Count > 0)
                        {
                            Program.ShowMessageDialog(result.Message);
                        }
                        else
                        {
                            ((IChildForm)ActiveMdiChild).RefreshData();
                        }
                    }
                    catch (Exception ex)
                    {
                        Program.ShowError(ex);
                    }
                    Program.StopProgress();
                    Program.ShowMessage("已完成保存数据");
                    btnSave.Enabled = true;
                }));
            }
        }

        //save all
        private void barButtonItem17_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnSaveAll.Enabled = false;
            Program.ShowMessage("开始保存数据...");
            Program.StartProgress();
            this.MdiChildren.ToList().ForEach(x =>
            {
                if (x is IChildForm)
                {
                    try
                    {
                        var result = ((IChildForm)x).Save();

                        if (result.Message.Count > 0)
                        {
                            Program.ShowMessageDialog(result.Message);
                        }
                        else
                        {
                            ((IChildForm)ActiveMdiChild).RefreshData();
                        }
                    }
                    catch (Exception ex)
                    {
                        Program.ShowError(ex);
                    }
                }
            });
            Program.StopProgress();
            Program.ShowMessage("已完成保存数据");
            btnSaveAll.Enabled = true;
        }

        //delete
        private void barButtonItem18_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                this.btnDeleteSelected.Enabled = false;
                Program.ShowMessage("开始删除数据...");
                Program.StartProgress();
                try
                {
                    var result = ((IChildForm)ActiveMdiChild).Delete();

                    if (result.Message.Count > 0)
                    {
                        Program.ShowMessageDialog(result.Message);
                    }
                    else
                    {
                        ((IChildForm)ActiveMdiChild).RefreshData();
                    }
                }
                catch (Exception ex)
                {
                    Program.ShowError(ex);
                }
                Program.StopProgress();
                Program.ShowMessage("已完成删除数据");
                btnDeleteSelected.Enabled = true;
            }
        }

        //refresh data
        private void barButtonItem19_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                this.btnReload.Enabled = false;
                Program.ShowMessage("读取数据...");
                Program.StartProgress();
                try
                {
                    var result = ((IChildForm)ActiveMdiChild).RefreshData();

                    if (result.Message.Count > 0)
                    {
                        Program.ShowMessageDialog(result.Message);
                    }
                }
                catch (Exception ex)
                {
                    Program.ShowError(ex);
                }
                btnReload.Enabled = true;
                Program.ShowMessage("读取数据完成");
                Program.StopProgress();
            }
        }

        //close
        private void barButtonItem20_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                ((IChildForm)ActiveMdiChild).Close();
            }
        }

        //民族
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var form = this.MdiChildren.FirstOrDefault(x => x is FormNation);
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormNation();
                form.MdiParent = this;
            }
            form.Show();
        }

        //职业
        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormProfession).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormProfession();
                form.MdiParent = this;
            }
            form.Show();
        }

        //行政区域
        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormAreas).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormAreas();
                form.MdiParent = this;
            }
            form.Show();
        }

        //政治信仰
        private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormPoliticalStatus).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormPoliticalStatus();
                form.MdiParent = this;
            }
            form.Show();
        }

        //人员关系
        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormRelation).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormRelation();
                form.MdiParent = this;
            }
            form.Show();
        }

        //户籍管理
        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormPersonList).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormPersonList();
                form.MdiParent = this;
            }
            form.Show();
        }

        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            barButtonItem14_ItemClick(null, null);
        }

        private void btnBack_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is INavigateForm)
            {
                ((INavigateForm)ActiveMdiChild).Back();
            }
        }

        private void btnForward_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is INavigateForm)
            {
                ((INavigateForm)ActiveMdiChild).Forward();
            }
        }

        private void btnAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            //var form = this.MdiChildren.Where(x => x is FormPerson).FirstOrDefault();
            //if (form != null)
            //{
            //    form.Activate();
            //    form.BringToFront();
            //}
            //else
            //{
            //    form = new FormPerson();
            //}
            var form = new FormPerson();
            if (form.ShowDialog() == DialogResult.OK)
            {
                var formPersonList = this.MdiChildren.Where(x => x is FormPersonList).FirstOrDefault();
                if (formPersonList != null && formPersonList.Visible)
                {
                    ((IChildForm)formPersonList).RefreshData();
                }
            }
        }
    }
}